package com.clgc.eduservice.shiro;

import com.clgc.eduservice.entity.EduTeacher;
import com.clgc.eduservice.service.EduTeacherService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class TeacherRealm extends AuthorizingRealm {

    @Autowired
    private EduTeacherService eduTeacherService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;

        String username = token.getUsername();
        //获取用户id
        int i = Integer.parseInt(username);
        //从数据库判断用户名是否存在
        //用户账号
        EduTeacher teacher = eduTeacherService.getById(i);
        token.setRememberMe(false);
        if (teacher == null) {
            System.out.println("用户不存在");
            return null;
        }
        token.setRememberMe(false);
        return new SimpleAuthenticationInfo(teacher,token.getPassword(),this.getName());
    }
}
